/*
* Copyright (C) 2024-2025 Uniontech Technology Co., Ltd.
*
* Author:      zhangbingyi <zhangbingyi@uniontech.com>
*
* Maintainer:  zhangbingyi <zhangbingyi@uniontech.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "securitylevelchangedialog.h"
#include "securitylevelchangedefinition.h"

//#include <DFontSizeManager>
//#include <DLabel>
//#include <DSuggestButton>
//#include <DTipLabel>

//#include <QScroller>
#include <QScrollBar>
#include <QScrollArea>
#include <QHBoxLayout>
#include <QPushButton>
//#include <DTitlebar>

SecurityLevelChangeDialog::SecurityLevelChangeDialog(int type, QWidget *parent)
    : DDialog(parent)
    , m_isFinishFlag(false)
{
    setAccessibleName("securityLevelChangeDialog");

    init(type);
}

SecurityLevelChangeDialog::~SecurityLevelChangeDialog()
{
}

void SecurityLevelChangeDialog::init(int type)
{
    Qt::WindowFlags flags = windowFlags();
    flags |= Qt::WindowStaysOnTopHint;
    setWindowFlags(flags);
//    setWindowFlag(Qt::WindowStaysOnTopHint);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    setModal(true);
    setAttribute(Qt::WidgetAttribute::WA_DeleteOnClose);
    setIcon(QIcon::fromTheme(DIALOG_TIP_YELLOW));

    // Dialog 尺寸大小
    setFixedWidth(420); // Dialog 宽度固定
    refreshDailogHeightSize(type); // Dialog 高度根据弹窗类型进行设置固定

    // 内容控件
    QWidget *contentWidget = new QWidget(this);
    QVBoxLayout *contentLayout = new QVBoxLayout;
    contentWidget->setLayout(contentLayout);
    contentWidget->setAccessibleName("securityLevelChangeDialog_contentWidget");
    contentLayout->setContentsMargins(0, 0, 0, 0);
    contentLayout->setSpacing(0);

//    // 设置标题
//    DLabel *title = new DLabel(contentWidget);
//    title->setAccessibleName("contentWidget_title");
//    title->setContentsMargins(20, 0, 0, 0);
//    title->setText(tr("Set up security levels of your system"));
//    title->setFixedWidth(400);
//    title->setAlignment(Qt::AlignCenter);
//    title->setElideMode(Qt::ElideMiddle);
//    QFont ft = title->font();
//    ft.setBold(true);
//    title->setFont(ft);
//    contentLayout->addWidget(title, 0, Qt::AlignCenter);
//    contentLayout->addSpacing(5);

    // 添加滑动条区域
    QScrollArea *scrollArea = new QScrollArea(contentWidget);
    scrollArea->setAccessibleName("contentWidget_scrollArea");
    scrollArea->viewport()->setAccessibleName("scrollArea_viewWidget");
    scrollArea->setContentsMargins(0, 0, 0, 0);
    scrollArea->setFrameShape(QFrame::Shape::NoFrame);
    scrollArea->setWidgetResizable(true);
    scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff);

//    // 触屏移动
//    QScroller::grabGesture(scrollArea->viewport(), QScroller::LeftMouseButtonGesture);
//    QScroller *scroller = QScroller::scroller(scrollArea);
//    QScrollerProperties sp;
//    sp.setScrollMetric(QScrollerProperties::HorizontalOvershootPolicy, QScrollerProperties::OvershootAlwaysOff);
//    scroller->setScrollerProperties(sp);

    // 设置滑动条区域背景颜色为透明
    QPalette pa = scrollArea->palette();
    pa.setBrush(QPalette::Window, Qt::transparent);
    scrollArea->setPalette(pa);

    // 设置文本文案
    QLabel *txt = new QLabel("", this);
    txt->setText(changeTxt(type)); // 根据类型设置文本文案内容
    txt->setAccessibleName("viewWidget_tipLable");
    txt->setWordWrap(true);
    txt->setFixedWidth(400);
    txt->setAlignment(Qt::AlignCenter);

    QWidget *scrollAreaWidget = new QWidget(scrollArea);
    QVBoxLayout *areaLayout = new QVBoxLayout;
    areaLayout->setContentsMargins(0, 0, 0, 0);

    scrollAreaWidget->setLayout(areaLayout);
    areaLayout->addWidget(txt);

    scrollArea->setWidget(scrollAreaWidget); // 添加到滑动条控件中
    contentLayout->addWidget(scrollArea);

    // 设置提示
    if (type == SecurityLevelChangeType::LtoMedium || type == SecurityLevelChangeType::HtoMedium) {
        QLabel *tipLabel = new QLabel("", contentWidget);
        tipLabel->setAlignment(Qt::AlignCenter);
        tipLabel->setWordWrap(true);
        tipLabel->adjustSize();
        tipLabel->setText(changeTip(type));
        tipLabel->setAccessibleName("securityLevelChangeDialog_tipLabel");
//        DFontSizeManager::instance()->bind(tipLabel, DFontSizeManager::T8); // 修改字号
        areaLayout->addWidget(tipLabel);
    }

    addContent(contentWidget);

    // 针对DTK弹窗目前accessibleName会随系统语言变化的情况
    // 通过insertButton的形式定制按钮,保持accessiableName的一致性

    QPushButton *cancelBtn = new QPushButton(this);
    cancelBtn->setText(tr("Cancel", "button"));
    cancelBtn->setObjectName("cancelBtn");
    cancelBtn->setAccessibleName("securityLevelChangeDialog_cancelButton");
    cancelBtn->setAttribute(Qt::WA_NoMousePropagation);
    this->insertButton(0, cancelBtn, false);

    QPushButton *okBtn = new QPushButton(this);
    okBtn->setText(tr("Confirm", "button"));
    okBtn->setObjectName("okBtn");
    okBtn->setAccessibleName("securityLevelChangeDialog_okButton");
    okBtn->setAttribute(Qt::WA_NoMousePropagation);
    this->insertButton(1, okBtn, false);

    connect(this, SIGNAL(buttonClicked(int, const QString &)), this, SLOT(onButtonClicked(int, const QString &)));
}

QString SecurityLevelChangeDialog::changeTxt(int nType)
{
    QString content;
    if (SecurityLevelChangeType::LMtoHigh == nType) {
        content = tr("The \"high\" security level enables privilege separation. Some user privileges will be limited, and some app functions that require elevated privileges will also be restricted. It may take a few minutes to complete the configuration. Once successful, your computer will restart. Please save data in advance. Are you sure you want to change it?");
    } else if (SecurityLevelChangeType::HtoMedium == nType) {
        content = tr("If changing it from \"high\" to \"medium\", the configuration of the \"high\" security level will be disabled, the user accounts (system admin, audit admin, and security admin) will be deleted, and the configuration of the \"medium\" security level will be implemented. Are you sure you want to change it?");
    } else if (SecurityLevelChangeType::LtoMedium == nType) {
        content = tr("If changing it from \"low\" to \"medium\", basic system protection will be implemented, and firewall protection will be enabled. Are you sure you want to change it?");
    } else if (SecurityLevelChangeType::HtoLow == nType) {
        content = tr("If changing it from \"high\" to \"low\", the configuration of the \"high\" security level will be disabled, the user accounts (system admin, audit admin, and security admin) will be deleted, and basic safety protection for trusted environments will be implemented. Are you sure you want to change it?");
    } else if (SecurityLevelChangeType::MtoLow == nType) {
        content = tr("If changing it from \"medium\" to \"low\", the configuration of the \"medium\" security level will be disabled, and basic safety protection for trusted environments will be implemented. Are you sure you want to change it?");
    }
    return content;
}

QString SecurityLevelChangeDialog::changeTip(int nType)
{
    QString tip;
    if (nType == SecurityLevelChangeType::LtoMedium || nType == SecurityLevelChangeType::HtoMedium) {
        tip = tr("Note: The password policy will be changed to \"medium\".");
    }
    return tip;
}

// 根据弹窗需求设置不同的固定值高度
void SecurityLevelChangeDialog::refreshDailogHeightSize(int nType)
{
    if (SecurityLevelChangeType::LMtoHigh == nType) {
        setFixedHeight(228);
    } else if (SecurityLevelChangeType::HtoMedium == nType) {
        setFixedHeight(230);
    } else if (SecurityLevelChangeType::LtoMedium == nType) {
        setFixedHeight(209);
    } else if (SecurityLevelChangeType::HtoLow == nType) {
        setFixedHeight(207);
    } else if (SecurityLevelChangeType::MtoLow == nType) {
        setFixedHeight(186);
    }
}

// 关闭窗口事件
void SecurityLevelChangeDialog::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
    if (!m_isFinishFlag) {
        Q_EMIT notifyDialogClose();
    }
    deleteLater();
}

void SecurityLevelChangeDialog::onButtonClicked(int index, const QString &text)
{
    Q_UNUSED(text);
    if (index == 1) {
        m_isFinishFlag = true;
        Q_EMIT notifyDialogFinish();
    }
    close();
}
